Skip to content

test(ast): diff the java and kotlin decoders against the golden capture - #391

Merged
Peefy merged 1 commit into
mainfrom
test/ast-diff-java-kotlin
Oct 6, 2026
Merged

Peefy merged 1 commit into
mainfrom
test/ast-diff-java-kotlin

Conversation

@Peefy

@Peefy Peefy commented Oct 6, 2026

Copy link
Copy Markdown
Contributor

Why

hack/ast_diff.rb runs each binding's real decoder over testdata/ast/alignment.json and diffs the result against the golden. It exists to catch the one bug the static check in ast-shape-test.yaml is blind to: a decoder that names every field correctly and then reads it one level of nesting wrong. That bug has shipped five times, and every occurrence produced undefined / None / "" rather than raising, so every per-language suite stayed green.

Twelve bindings are covered. Two more were listed in bindings.rb as unrunnable, and both reasons were wrong — they said "no JRE on this machine", which is a fact about one laptop rather than about the binding.

What this adds

java — decodes with the same Jackson classes every other JVM binding uses, so hack/dump/java/Dump.java walks them reflectively and reads each field's name off the @JsonProperty the decoder was given. The binding's own tag comes from the @JsonSubTypes table on the base class, which covers the As.PROPERTY bases (Stmt/Expr/Type) and the As.EXTERNAL_PROPERTY ones (LiteralTypeValue/NumberLitValue) without a special case each.

kotlin — compiles those same com.kcl.ast sources, so the shared decoder was never the part in doubt. What is kotlin's own is AstWire.kt, 649 lines of hand-written shape restoration written to mirror dotnet/KclLib.AST/Wire.cs, and AstBuild.kt's New* constructors. 1370 lines with nothing checking them, and the round-trip is the only thing that can.

Result

Neither binding differs from the golden, so CLASS_TAGS gains the eleven wrapper classes java adds for a payload the wire has no name for (UnionType.UnionTypeValue and the rest, one level deeper again for the literal arms). kotlin and dotnet collapse the same shapes on the write side, which is why they never needed naming; java has no writer, so the reflective walk meets the wrapper where it stands.

12 agreed, 0 disagreed, 0 failed to run, 0 skipped, 0 vacuous

java reaches 1012/1012 golden objects with R4 ×72. kotlin reaches 1012/1012 with R4 ×61 and R12 ×321 — the same counts dotnet and zig produce, which is the evidence the two mirror files really are in sync.

Worth knowing: java has no parseModule

JsonUtil exposes exactly one decoder, deserializeProgram, which wants the {"root":…,"pkgs":{…}} envelope that ParseProgramResult.ast_json carries. ParseFileResult.ast_json is a bare Module, so a Java caller who parses one file and asks for the typed AST has to build an ObjectMapper themselves. Every other binding here has both entry points. Not fixed here — out of scope for a test-only change — but it is the reason the dumper reads Module directly.

CI

Both legs run on the default ubuntu runner and build nothing: the dumpers compile the AST sources directly, so unlike java-test.yaml and kotlin-test.yaml there is no JNI shim and no Rust toolchain. Both scripts pin javac --release 8 to the language level the poms declare, so a Java 9+ API is a compile error in the harness rather than something that passes on a new JDK and fails on the zulu 8 the build matrix uses.

Two things that would have failed on a cold runner, found and fixed while wiring the legs up: resolving the declared classpath through maven has to be online (-o fails against an empty ~/.m2, as a classpath error before the dumper has even run), and kotlin-compiler is a plugin dependency, so a fresh local repository does not have it — hack/dump/kotlin.sh now fetches it itself, with a workflow step doing the same so the failure is attributable.

Still uncovered

go, c and cpp remain without a dumper. go's reason is structural and accurate — it has no typed AST package at all. For c the reason holds too: KCL_DECLARE_NODE makes every node a void*, so a dumper would be a hand-written second traversal. The cpp reason is inaccurate — cpp/include/kcl_ast.hpp is 1987 lines of its own typed AST with a virtual tag() on every node, not a wrapper over the C header — but a dumper there would still need a per-class member list, which is the maintainability objection wearing the wrong explanation. Probing c and cpp directly on the golden (body=63, comments=33, all 33 non-empty) found neither broken.

🤖 Generated with Claude Code

@Peefy
Peefy force-pushed the test/ast-diff-java-kotlin branch from ff05ad1 to aaf8435 Compare October 6, 2026 07:34
Both were listed in `bindings.rb` as unrunnable, and both reasons were
wrong. They said "no JRE on this machine", which is an observation about
one laptop rather than about the binding — a JDK is all they need, and
`ast_diff.rb` has no way to run either decoder until now.

java decodes the capture with the same Jackson classes every other JVM
binding uses, so the dumper walks them reflectively and reads each field's
name off the `@JsonProperty` the decoder was given, rather than from a
list written out here. Its own tag comes from the `@JsonSubTypes` table
on the base class, which covers both the `As.PROPERTY` bases
(`Stmt`/`Expr`/`Type`) and the `As.EXTERNAL_PROPERTY` ones
(`LiteralTypeValue`/`NumberLitValue`) without a special case each.

kotlin compiles those same `com.kcl.ast` sources, so the decoder it
shares with java was never the part in doubt. What is kotlin's own is
`AstWire.kt` — 649 lines of hand-written shape restoration, written to
mirror `dotnet/KclLib.AST/Wire.cs`, whose docstring claims a decode/encode
cycle is a fixed point — and `AstBuild.kt`'s `New*` constructors. 1370
lines with nothing checking them, and the round-trip is the only thing
that can. It passes, and its reduction counts (R4 x61, R12 x321) come out
identical to dotnet's and zig's, which is the evidence the two mirror
files really are in sync.

Neither binding introduces a real difference against the golden, so
CLASS_TAGS gains the eleven wrapper classes java adds for a payload the
wire has no name for (`UnionType.UnionTypeValue` and the rest, a level
deeper again for the literal arms). kotlin and dotnet collapse the same
shapes on the write side, which is why they never needed naming; java has
no writer, so the reflective walk meets the wrapper where it stands.

Worth knowing from the java dumper, recorded here rather than only in the
dumper's header: the binding has no `parseModule`. `JsonUtil` exposes
exactly one decoder, `deserializeProgram`, which wants the
`{"root":…,"pkgs":{…}}` envelope that `ParseProgramResult.ast_json`
carries — but `ParseFileResult.ast_json` is a bare `Module`, so a caller
who parses one file and asks for the typed AST has to build an
ObjectMapper themselves. Every other binding here has both entry points.

The CI legs build nothing: unlike java-test and kotlin-test, the dumpers
compile the AST sources directly, so no JNI shim and no Rust toolchain.
Both scripts pin `javac --release 8` to the language level the poms
declare, so a Java 9+ API is a compile error here rather than something
that passes on a new JDK and fails on the zulu 8 the build matrix uses.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Signed-off-by: Peefy <xpf6677@163.com>
@Peefy
Peefy force-pushed the test/ast-diff-java-kotlin branch from aaf8435 to 486fab3 Compare October 6, 2026 07:54
@Peefy
Peefy merged commit 7a99b18 into main Oct 6, 2026
17 of 18 checks passed
@Peefy
Peefy deleted the test/ast-diff-java-kotlin branch October 6, 2026 11:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant